package org.xyl.context;

import java.util.*;

/**
 * 会话上下文 - 存储对话历史和会话相关信息
 */
public class SessionContext {
    private String sessionId;
    private List<Message> messageHistory;
    private long createdAt;
    private long lastAccessedAt;
    
    public SessionContext(String sessionId) {
        this.sessionId = sessionId;
        this.messageHistory = new ArrayList<>();
        this.createdAt = System.currentTimeMillis();
        this.lastAccessedAt = System.currentTimeMillis();
    }
    
    /**
     * 添加用户查询到对话历史
     */
    public void addUserQuery(String query) {
        messageHistory.add(new Message(MessageType.USER, query));
        this.lastAccessedAt = System.currentTimeMillis();
    }
    
    /**
     * 添加系统回复到对话历史
     */
    public void addSystemResponse(String response) {
        messageHistory.add(new Message(MessageType.SYSTEM, response));
        this.lastAccessedAt = System.currentTimeMillis();
    }
    
    /**
     * 获取最近的N条消息
     */
    public List<Message> getRecentMessages(int limit) {
        int startIndex = Math.max(0, messageHistory.size() - limit);
        return messageHistory.subList(startIndex, messageHistory.size());
    }
    
    public String getSessionId() {
        return sessionId;
    }
    
    public long getLastAccessedAt() {
        return lastAccessedAt;
    }
    
    /**
     * 消息类型枚举
     */
    public enum MessageType {
        USER,
        SYSTEM
    }
    
    /**
     * 消息类
     */
    public static class Message {
        private MessageType type;
        private String content;
        private long timestamp;
        
        public Message(MessageType type, String content) {
            this.type = type;
            this.content = content;
            this.timestamp = System.currentTimeMillis();
        }
        
        public MessageType getType() {
            return type;
        }
        
        public String getContent() {
            return content;
        }
        
        public long getTimestamp() {
            return timestamp;
        }
    }
}